<?php

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\BindModel;
use bc\model\ItemModel;
use bc\model\OrderbindMode;
use bc\model\OrderModel;
use bc\model\ReportModel;
use bc\model\UserreportModel;
use bc\model\wc\SpreadModel;
use bc\util\Util;
use yii;
use bc\util\backend\Page;
use yii\web\Session;

class VSampleController extends BackendController
{
    public function actionIndex()
    {
        $model = new BindModel();
        $page = $this->get('page', 1);
        $params = array();
        //$params=array('bc_item_data.item_product_id'=>1);

        $order_no = $this->get('order_no', '');
        $bind_no = $this->get('bind_no', '');
        $product_id = $this->get('product_id', 3);
        if ($order_no != '') {
            $params = ['like', 'bc_order.order_no', $order_no];
        }
        if ($bind_no != '') {
            $params = ['like', 'bc_order_bind.bind_no', $bind_no];
        }
        if ($product_id != '') {
            $params = ['like', 'bc_order_bind.product_id', $product_id];
        }
        $model->sort='bc_order.create_time desc';
        $list = $model->getReportList($params, $page, self::PAGE_SIZE);

        //给数据添加序号
        $tempid = -1;
        foreach ($list as $k => $v) {
            if ($v['order_id'] != $tempid) {
                $tempid = $v['order_id'];
                $size = 1;
            }
            $list[$k]['size'] = $size++;
        }

        $total = $model->getTotal($params);

        $pagination = new Page($total, self::PAGE_SIZE);

//        $view['order_no'] = $order_no;
//        $view['bind_no'] = $bind_no;
//        $view['list'] = $list;
//        $view['pagination'] = $pagination;
        return $this->render('index', array('list' => $list, 'Pagination' => $pagination, 'order_no' => $order_no, 'bind_no' => $bind_no));
    }

//    //ajax
//    public function  actionAjaxlrno()
//    {
//        $bind_id = $this->get("bind_id");
//        $bind_no = $this->get("bind_no");
//
//        //验证采样管编号
//        $szapi = new SzApi();
//        $szapiData = json_decode($szapi->searchInfo($bind_no),true);
//        if($szapiData['retCode'] !== 0)
//        {
//            $data   =   array(
//                'error'  =>    '1',
//                'msg'     =>  '验证失败，请检查录入编号！'
//            );
//            echo json_encode($data);
//            die();
//        }
//
//		$session = new Session ();
//		$shop_user = $session->get ( 'admin' );
//		$account = $shop_user ['account'];
//
//        $model = new BindModel();
//        if ($bind_no || $bind_id) {
//            $model->modify(array(
//            	'bind_no' => $bind_no,
//				"bind_status" => 2,
//				"create_time" => time(),
//			), $bind_id);
//
//            $info = $model->getRow(['bind_id'=>$bind_id]);
//			$orderModel = new OrderModel();
//			$orderModel->modify([
//				'send_time'	=>	time(),
//				'send_user'	=>	$account,
//			], $info['order_id']);
//        }
//
//        $model->autoId = 'bind_id';
//        $bindInfo = $model->getOrderInfoByBindid($bind_id);
//        echo json_encode($bindInfo);
//    }

    public function actionSampleinfo()
    {
        $bind_id = $this->get("bind_id");
        $size = $this->get('size');
        $model = new BindModel();
        $sampleinfo = array();
        $sampleinfo = $model->getSampleInfo($bind_id);
        $sampleinfo['size'] = $size;

        //根据bind_no获取产品id
        $OrderbindM= new OrderbindMode();
//        $OrderbindM->columns='bc_order_bind.user_id as ouser_id,bc_order_bind.product_id as oproduct_id,bc_order_bind.bind_no as obind_no';
        $OrderbindM->autoId='bind_id';
        $OrderbindD=$OrderbindM->getRowById($bind_id);
        $bind_no=$OrderbindD['bind_no'];
        $user_id=$OrderbindD['user_id'];
        $product_id = $OrderbindD['product_id'];
        $ItemM = new ItemModel();
        $ItemD = $ItemM->getUserInfoByUseridAndProductId($user_id,$product_id,$bind_no);
        foreach($ItemD as $k => $value){
            if($value['biitem_type']==2 || $value['biitem_type']==3 || $value['biitem_type']==4){
                if(empty($data[$value['report_key']])){
                    if($value['bsitem_type'] == 1) {
                        $data[$value['report_key']] = $value['subitem_value'];
                    }else{
                        $data[$value['report_key']] = $value['subitem_name'];
                    }
                }else {
                    if($value['bsitem_type'] == 1){
                        $data[$value['report_key']] = $data[$value['report_key']] . ";" . $value['subitem_value'];
                    }else{
                        $data[$value['report_key']] = $data[$value['report_key']] . ";" . $value['subitem_name'];
                    }

                }

            }else{
                $data[$value['report_key']]=$value['item_value'];
            }
        }
        $sampleinfo['Data'] = $data;

        return $this->render('sampleinfo', array('sampleinfo' => $sampleinfo));
    }

    public function actionEdit()
    {
        $view['bind_id']=$this->get('bind_id');
        $bindModel = new BindModel();
        $row = $bindModel->getRowById($view['bind_id']);

        $UserreportM = new UserreportModel();
        $UserreportD=$UserreportM->getList(['bind_no'=>$row['bind_no']]);
        $view['userreport'] = array_column($UserreportD, 'report_value', 'report_key');
        return $this->render('edit',$view);
    }

    //后台编辑数据入库
    public function actionSave()
    {
        $bind_id = $this->post('bind_id');
        $UserreportM = new UserreportModel();

        $reportData = array();
        $reportData['Bsm1'] = $this->post('Bsm1',0);
        $reportData['sBsm1'] = $this->post('sBsm1',0);
        $reportData['Fork1'] = $this->post('Fork1',0);
        $reportData['sFork1'] = $this->post('sFork1',0);
        $reportData['Apa1'] = $this->post('Apa1',0);
        $reportData['sApa1'] = $this->post('sApa1',0);
        $reportData['Taq1'] = $this->post('Taq1',0);
        $reportData['sTaq1'] = $this->post('sTaq1',0);
        $reportData['report_1_value'] =$this->post('report_1_value',0);

        $reportModel = new ReportModel();
        $bindModel = new BindModel();
        $row = $bindModel->getRowById($bind_id);
        $reportInfo = new \stdClass();
        $reportInfo->user_id = $row['user_id'];
        $reportInfo->bind_no = $row['bind_no'];
        $reportInfo->product_id = $row['product_id'];
        //入库前先删除原有报告数据
        $UserreportM->removeAll(['bind_no'=>$row['bind_no']]);
        $reportModel->tractionInsert($reportData, $reportInfo);
        $bindModel->modify(['bind_status' => 5], $bind_id);
        return $this->redirect('/v-sample/sampleinfo?bind_id='.$bind_id);
    }

    //送检状态
    public function actionSubmitted()
    {
        $bind_id = $this->get('bind_id');
        $model = new BindModel();
        //判断当前是不是绑定状态
        $model->autoId='bind_id';
        $dataM=$model->getRowById($bind_id);
        if($dataM['bind_status'] == 3){
            $data=array('bind_status'=>4);
            $model->modify($data,$bind_id);
        }
        return $this->redirect('/v-sample');
    }
}

